import * as vscode from 'vscode';
import * as NSNodePath from "path";
import { extensionId, getCommandId } from './extension-basic';
import {
  createVueComponentFiles,
  ScriptSuffix,
  StyleLang,
} from "./vue-component-helper";
import {
  getIsPropComponentName,
  toKebabCase
} from "./string-util";

export interface InputBoxHideStatus {
  isAccept: boolean,
  value: string,
}


export async function createVueComponent(componentName: string, uri?: vscode.Uri): Promise<void> {
  if (!getIsPropComponentName(componentName)) {
    vscode.window.showInformationMessage("组件名不合规: " + componentName);
    return;
  }
  let componentDirPath: string = "";
  if (!uri) {
    const { value,  isAccept } = await createInputBox("请输入文件存放目录(相对vscode工作目录的相对路径或绝对路径)");
    if (!isAccept) {
      return;
    }
    if (NSNodePath.isAbsolute(value)) {
      componentDirPath = value;
    } else if (vscode.workspace.workspaceFolders) {
      componentDirPath = NSNodePath.join(vscode.workspace.workspaceFolders[0].uri.fsPath, value);
    } else {
      return;
    }
  } else {
    const fsStat = await vscode.workspace.fs.stat(uri);
    if (fsStat.type === vscode.FileType.File) {
      componentDirPath = NSNodePath.join(
        NSNodePath.dirname(uri.fsPath),
        "components",
      );
    } else if (fsStat.type === vscode.FileType.Directory) {
      componentDirPath = uri.fsPath;
    } else {
      vscode.window.showInformationMessage("错误的路径:" + uri.fsPath);
      return;
    }
    // componentDirPath = uri.fsPath;
  }
  const styleSuffix = vscode.workspace.getConfiguration(extensionId).get<StyleLang>("style.suffix") || "scss";
  const scriptSuffix = vscode.workspace.getConfiguration(extensionId).get<ScriptSuffix>("script.suffix") || "ts";
  const {
    vueFilePath
  } = await createVueComponentFiles(
    componentDirPath,
    componentName,
    toKebabCase(componentName),
    styleSuffix,
    scriptSuffix
  );
  await vscode.window.showTextDocument(
    vscode.Uri.file(vueFilePath)
  );
}

export function createInputBox(placeholder: string, func?: (inputBox: vscode.InputBox) => void): Promise<InputBoxHideStatus> {
  return new Promise<InputBoxHideStatus>((resolve) => {
    if (!func && typeof vscode.window.showInputBox === "function") {
      vscode.window.showInputBox({
        placeHolder: placeholder,
      }).then((value) => {
        resolve({
          value: value || "",
          isAccept: value !== undefined
        });
      });
      return;
    }
    const inputBox = vscode.window.createInputBox();
    let isAccept = false;
    inputBox.placeholder = placeholder;
    if (func) {
      func(inputBox);
    }
    inputBox.onDidAccept(() => {
      isAccept = true;
      resolve({
        value: inputBox.value,
        isAccept,
      });
    });
    inputBox.onDidHide(() => {
      if (!isAccept) {
        resolve({
          value: inputBox.value,
          isAccept,
        });
      }
    });
    inputBox.show();
  });
}


export function onCreateVueComponentActive(context: vscode.ExtensionContext) {
  const disposableCreateVueComponent = vscode.commands.registerCommand(getCommandId("createVueComponent"), (uri?: vscode.Uri) => {
    createInputBox("请输入Vue组件名").then((status) => {
      if (status.isAccept) {
        return createVueComponent(status.value, uri);
      }
    }).catch((err) => {
      console.error(err);
    });		
	});
	context.subscriptions.push(disposableCreateVueComponent);
}